package cn.wizzer.modules.controllers.platform.store;

import cn.wizzer.common.annotation.SLog;
import cn.wizzer.common.base.Result;
import cn.wizzer.common.filter.PrivateFilter;
import cn.wizzer.common.page.DataTableColumn;
import cn.wizzer.common.page.DataTableOrder;
import cn.wizzer.modules.models.store.Store;
import cn.wizzer.modules.models.sys.Sys_user;
import cn.wizzer.modules.services.store.StoreMiddleService;
import cn.wizzer.modules.services.store.StoreService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@IocBean
@At("/platform/store/complete")
@Filters({@By(type = PrivateFilter.class)})
public class StoreCompleteController {
	private static final Log log = Logs.get();
	@Inject
	private StoreService storeService;
	@Inject
	private StoreMiddleService storeMiddleService;

	@At("")
	@Ok("beetl:/platform/store/store/index.html")
	@RequiresAuthentication
	public void index(HttpServletRequest req) {
		req.setAttribute("mode","complete");
	}

	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String sql = "select a.* from store a,store_middle b where a.id=b.storeId and b.state=3";
		sql +=" and b.opBy='"+user.getId()+"'";
		return storeService.data(length, start, draw, Sqls.create(sql), Sqls.create(sql));
    }

    @At
    @Ok("beetl:/platform/store/store/add.html")
    @RequiresAuthentication
    public void add() {

    }

    @At
    @Ok("json")
    @SLog(tag = "新建Store", msg = "")
    public Object addDo(@Param("..") Store store, HttpServletRequest req) {
		try {
			storeService.insert(store);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
    }

    @At("/edit/?")
    @Ok("beetl:/platform/store/store/edit.html")
    @RequiresAuthentication
    public Object edit(String id) {
		return storeService.fetch(id);
    }

    @At
    @Ok("json")
    @SLog(tag = "修改Store", msg = "ID:${args[0].id}")
    public Object editDo(@Param("..") Store store, HttpServletRequest req) {
		try {

			store.setOpAt((int) (System.currentTimeMillis() / 1000));
			storeService.updateIgnoreNull(store);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
    }


    @At({"/delete","/delete/?"})
    @Ok("json")
    @SLog(tag = "删除Store", msg = "ID:${args[2].getAttribute('id')}")
    public Object delete(String id, @Param("ids") String[] ids ,HttpServletRequest req) {
		try {
			if(ids!=null&&ids.length>0){
				storeService.delete(ids);
    			req.setAttribute("id", org.apache.shiro.util.StringUtils.toString(ids));
			}else{
				storeService.delete(id);
    			req.setAttribute("id", id);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
    }


    @At("/detail/?")
    @Ok("beetl:/platform/store/store/detail.html")
    @RequiresAuthentication
	public Object detail(String id) {
		if (!Strings.isBlank(id)) {
			return storeService.fetch(id);

		}
		return null;
    }

}
